﻿using Common.Communication;
using Common.Device.DeviceDb;
using Common.Device.Interfaces;
using Common.Tool.DataTools;
using Modbus;
using Modbus.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaiJiMoKuai.WenDu
{
    public class RSWSN012DSerialPort : ModbusRtuClient, ICaiJiMoKuai
    {
        byte station;
        public RSWSN012DSerialPort(UseDevice device) : base(device.name, device.port, Convert.ToInt32(device.parameter), 1500, EndianFormat.ABCD, Convert.ToByte(device.addr,16), false)
        {
            station = Convert.ToByte(device.addr);
        }
        public Result<double> GetValue(string channel)
        {
            lock (this)
            {
                var result = new Result<double>();
                var read = new Result<Int16>();
                AppendInfo(Name, $"读取当前{channel}");
                read = Read<Int16>($"s={station};x=3;{(channel == "湿度" ? 0 : 1)}");
                if (read.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = $"读取当前{channel}失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = read.Value / 10.0;
                AppendInfo(Name, DataConvert.ToUnit(result.Value, "F3") + (channel == "湿度" ? "%RH" : "℃"));
                return result.EndTime();
            }
        }
    }
}
